home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.orig.lzh / notebook / newslock < prev    next >
Text File  |  1989-06-27  |  3KB  |  86 lines

  1. .DA "22 April 1989"
  2. .TL
  3. Locking in C News
  4. .AU
  5. Henry Spencer
  6. .AI
  7. Dept. of Zoology
  8. University of Toronto
  9. .LP
  10. Several parts of C News need some way of locking parts of the news
  11. subsystem against concurrent execution.
  12. Various system-specific locking system calls exist, but none of them
  13. is truly portable, and most of them provide far more functionality than
  14. we need.
  15. .LP
  16. C News locking uses the \fIlink\fR(2) system call and pre-agreed names.
  17. \fILink\fR has the necessary characteristic for safe locking:
  18. it is an atomic test-and-set operation.
  19. Furthermore, it exists in all Unixes.
  20. .LP
  21. All locks are created in the NEWSCTL directory
  22. (see \fIConfiguration Mechanisms in C News\fR for where this directory
  23. is to be found and how programs can determine this)
  24. and have names starting with `LOCK'.
  25. To acquire a lock, first create a temporary file in NEWSCTL with a name
  26. of the form `L.\fIn\fR', where \fIn\fR is your process id.
  27. You are urged to also write your process id, in decimal ASCII, into this
  28. file.
  29. Then attempt to link the
  30. temporary
  31. file to `LOCK\fIx\fR', where \fIx\fR is chosen based
  32. on what sort of locking you wish to do.
  33. Existing lock names are:
  34. .TS
  35. center;
  36. ll.
  37. LOCK    relaynews, modifications to control files
  38. LOCKinput    input subsystem processing spooled input
  39. LOCKbatch    batcher preparing batches
  40. LOCKexpire    expire expiring articles
  41. .TE
  42. If the link fails, sleep and try again.
  43. If it succeeds, proceed.
  44. The temporary file may be removed then or at the same time as the lock
  45. is removed.
  46. Programs are expected to make a determined effort to remove lock files
  47. when they terminate, normally or as a result of signals.
  48. .LP
  49. Shell programs have an additional problem in that System V has broken
  50. \fIln\fR(1) so that it removes a pre-existing destination file.
  51. C News therefore provides a pure, simple locking program under
  52. the name NEWSBIN/newslock (if the recommendations in
  53. \fIDirectory Layout and PATH in C News\fR are followed, this will
  54. automatically be in the search path of shell programs).
  55. Usage is `newslock\ tempfile\ lockfile';
  56. exit status is 0 for success, 1 for failure, 2 for wrong number of arguments.
  57. No messages are printed for normal failure, so no redirection of output
  58. is needed.
  59. .LP
  60. A suitable locking procedure for a shell file using the standard
  61. configuration facilities is:
  62. .DS
  63. lock="$NEWSCTL/LOCKxxx"        # modify name as appropriate
  64. ltemp="$NEWSCTL/L.$$"
  65. echo $$ >$ltemp
  66. trap "rm \-f $ltemp ; exit 0" 0 1 2 15
  67. while true
  68. do
  69.     if newslock $ltemp $lock
  70.     then
  71.         trap "rm \-f $ltemp $lock ; exit 0" 0 1 2 15
  72.         break
  73.     fi
  74.     sleep 30
  75. done
  76. .DE
  77. A template of this form can be found in the file \fInewslock.sh\fR.
  78. .LP
  79. Although there are various thorny questions associated with breaking
  80. locks by dead programs, reboot is a time when surviving locks are
  81. definitely invalid.
  82. (Although there are problems even here if a networked group of systems
  83. are not rebooted as a unit.)
  84. For this and other reasons, a system running C News should execute
  85. NEWSCTL/bin/newsboot at reboot time (e.g. from \fI/etc/rc\fR).
  86.